﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace MakingOutputOfTestCases
{
    public partial class MainWindow : Window
    {
        string exePath;
        string IODir;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSelectExe_Click(object sender, RoutedEventArgs e)
        {
            string initialDir = (string)Microsoft.Win32.Registry.CurrentUser.GetValue("ExeDirectory");
            OpenFileDialog outputFile = new OpenFileDialog();
            outputFile.InitialDirectory = initialDir;
            if (outputFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.exePath = outputFile.FileName;
                txtExePath.Text = this.exePath;
                initialDir = System.IO.Path.GetDirectoryName(this.exePath);
                Microsoft.Win32.Registry.CurrentUser.SetValue("ExeDirectory", initialDir);
            }
        }

        private void btnSelectInputDir_Click(object sender, RoutedEventArgs e)
        {
            string initialDir = (string)Microsoft.Win32.Registry.CurrentUser.GetValue("TestDirectory");
            OpenFileDialog outputFile = new OpenFileDialog();
            outputFile.InitialDirectory = initialDir;
            if (outputFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.IODir = outputFile.FileName;
                IODir = System.IO.Path.GetDirectoryName(IODir);
                Microsoft.Win32.Registry.CurrentUser.SetValue("TestDirectory", IODir);
                txtInputDir.Text = this.IODir;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                System.Diagnostics.ProcessStartInfo scanProcess = new System.Diagnostics.ProcessStartInfo();
                scanProcess.RedirectStandardInput = true;
                scanProcess.RedirectStandardOutput = true;
                scanProcess.FileName = exePath;
                scanProcess.UseShellExecute = false;

                Process process = Process.Start(scanProcess);

                string fileName = string.Format("{0}\\test_{1:00}.in.txt", IODir, i);
                if (!File.Exists(fileName))
                {
                    System.Windows.Forms.MessageBox.Show("Missing file");
                }

                using (StreamReader sr = new StreamReader(fileName))
                {
                    using (StreamWriter sw = process.StandardInput)
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            sw.WriteLine(line);
                        }
                    }
                }

                using (StreamWriter sw = new StreamWriter(string.Format("{0}\\test_{1:00}.out.txt", IODir, i)))
                {
                    using (StreamReader sr = process.StandardOutput)
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            sw.WriteLine(line);
                        }
                    }
                }
            }
        }
    }
}
